package com.dutao.controller;

import com.dutao.common.Result;
import com.dutao.entity.DataCount;
import com.dutao.entity.RegionStates;
import com.dutao.service.CovidDataProvider;
import com.dutao.util.ChinaStateSwitch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
public class IndexController {

    @Autowired
    private CovidDataProvider covidDataProvider;

    @GetMapping("/{country}")
    public Result search(@PathVariable("country") String country){
        List<RegionStates> res = covidDataProvider.getData().stream()
                .filter(rs -> rs.getCountry().contains(country))
                .collect(Collectors.toList());
        if(res.size()!=0)
            return Result.succes(res);
        return Result.fail("查不到");
    }

    @GetMapping("/all")
    public Result index() {
        return Result.succes(covidDataProvider.getData());
    }

    @PostMapping("/all")
    public Result getData(){
        List<RegionStates> data = covidDataProvider.getData();
        return Result.succes(new DataCount()
                .setSum(data.stream().mapToInt(RegionStates::getLatestTotalCases).sum())
                .setAdd(data.stream().mapToInt(RegionStates::getDiffFromPrevDay).sum())
                .setDeath((int)covidDataProvider.getDataMap().get("death"))
                .setRecovered((int)covidDataProvider.getDataMap().get("recover")));
    }

    @PostMapping("/china")
    public Result getChinaCount(){
        List<RegionStates> china = covidDataProvider.getData().stream()
                .filter(rs -> rs.getCountry().equals("China")||rs.getCountry().contains("Taiwan"))
                .collect(Collectors.toList());
        return Result.succes(new DataCount()
                .setSum(china.stream().mapToInt(RegionStates::getLatestTotalCases).sum())
                .setAdd(china.stream().mapToInt(RegionStates::getDiffFromPrevDay).sum())
                .setDeath((int)covidDataProvider.getDataMap().get("cdeath"))
                .setRecovered((int)covidDataProvider.getDataMap().get("crecover")));
    }

    @GetMapping("/china")
    public Result china()throws Exception{
        List<RegionStates> china = covidDataProvider.getData().stream()
                .filter(rs -> rs.getCountry().equals("China")||rs.getCountry().contains("Taiwan"))
                .collect(Collectors.toList());
        List<RegionStates> temp = new ArrayList<>();
//        temp.addAll(china);深拷贝
        china.forEach(rs->{ temp.add(new RegionStates()
                    .setState(ChinaStateSwitch.getName(rs.getState()))
                    .setCountry(rs.getCountry())
                    .setLatestTotalCases(rs.getLatestTotalCases())
                    .setDiffFromPrevDay(rs.getDiffFromPrevDay())
            );
        });
        temp.forEach(rs->{
            if(rs.getCountry().contains("Taiwan")){
                rs.setState("台湾").setCountry("中国");
            }
        });
        return Result.succes(temp);
    }

    @PostMapping("/data/seven")
    public Result getSeven(){
        return Result.succes(covidDataProvider.getDataMap());
    }

    @GetMapping("/data/{type}")
    public Result getSevenData(@PathVariable("type") String type){
        return Result.succes(covidDataProvider.getDataMap().get(type));
    }




}
